package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import com.itheima.utils.HealthStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wgq
 * @date 2019-10-06 21:45
 */

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 新增检查项
     *
     * @param checkItem
     */
    @Override
    public void add(CheckItem checkItem) {
        if (checkItemDao.selectByCode(checkItem.getCode()) > 0) {
            throw new RuntimeException("项目编码已存在！");
        }
        if (checkItemDao.selectByName(checkItem.getName()) > 0) {
            throw new RuntimeException("项目名称已存在！");
        }
        checkItemDao.add(checkItem);
    }

    /**
     * 分页查询检查项
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        if(null == currentPage || currentPage.intValue() <=0){
            currentPage = 1;
        }
        Integer pageSize = queryPageBean.getPageSize();
        if(null == pageSize || pageSize.intValue() <=0){
            pageSize = 10;
        }
        String queryString = queryPageBean.getQueryString();
        if(HealthStringUtils.isEmpty(queryString)){
            queryString="";
        }else{
            queryString = queryString.trim();
        }
        //完成分页查询，基于Mybatis框架提供的分页助手插件完成
        //PageHelper底层是基于线程绑定，会把 currentPage,pageSize 这些数据绑定到当前线程，
        //会从当前线程取出数据拼接到我们的SQL后面。所以这个方法不需要返回值
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemDao.selectByCondition("%"+queryString+"%");
        long total = page.getTotal();
        List<CheckItem> result = page.getResult();
        //SELECT * FROM t_checkitem LIMIT 1,10
        return new PageResult(total, result);
    }

    /**
     * 根据ID删除检查项
     *
     * @param id
     * @return
     */
    @Override
    public void deleteCheckItemById(Integer id) {
        //判断当前检查项是否关联到检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count > 0) {
            //当前检查项不关联在检查组，不能删除，抛出一个异常
            throw new RuntimeException("当前检查项被检查组引用，不能删除");
        }
        //可以删除
        checkItemDao.deleteCheckItemById(id);
    }

    /**
     * 根据ID查询检查项
     *
     * @param id
     * @return
     */
    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findById(id);
        return checkItem;
    }

    /**
     * 编辑更新检查项的内容
     *
     * @param checkItem
     */
    @Override
    public void editCheckItem(CheckItem checkItem) {
        if(checkItemDao.findItemName(checkItem)>0){
            throw new RuntimeException("检查项名称已存在，请修改...");
        }
        checkItemDao.editCheckItem(checkItem);
    }

    /**
     * 查询所有检查项，用户检查组新建弹出窗口的展示
     *
     * @return
     */
    @Override
    public List<CheckItem> findAllItem() {
        return checkItemDao.findAllItem();
    }


}
